package com.test.account.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.account.entity.Account;
import com.test.account.mapper.AccountMapper;
import com.test.account.service.AccountService;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
    @Override
    public void debit(String userId, int money) {
        Integer currentMoney = getOne(Wrappers.<Account>lambdaQuery()
                .select(Account::getMoney)
                .eq(Account::getId, userId)
                .last("for update"))
                .getMoney();
        if (money <= currentMoney) {
            update(Wrappers.<Account>lambdaUpdate().set(Account::getMoney, currentMoney - money));
        } else {
            throw new RuntimeException("账户余额不足...");
        }
    }
}
